const tools = {};

// 后退
tools.openimg = (src) => {
	const arr = []
	arr[0] = src
	uni.previewImage({
		urls: arr
	});
};
// 后退
tools.back = (step) => {
	uni.navigateBack({
		delta: step
	});
};

// 复制
tools.copy = (val) => {
	uni.setClipboardData({
		data: val,
		success() {
			uni.showToast({
				title: "复制成功",
				duration: 2000,
				icon: "none"
			})
		}
	})
}

//弹出消息
tools.toast = (val) => {
	uni.showToast({
		title: val,
		icon: "none",
		duration: 2000
	})
}

tools.modal = (val) => {
	uni.showModal({
		content: val,
		showCancel: false
	});
}

//页面跳转
tools.navigite = (path, value, value1, value2, value3, value4, value5) => {
	uni.navigateTo({
		url: path + '?value=' + value + "&value1=" + value1 + "&value2=" + value2 + "&value3=" + value3 +
			"&value4=" + value4 + "&value5=" + value5
	})
}

//关闭当前页面，跳转到应用内的某个页面
tools.navigiteTo = (path) => {
	uni.redirectTo({
		url: path
	})
}

//关闭当前页面，跳转到tabbar页面
tools.navigiteToTab = (path) => {
	uni.switchTab({
		url: path
	})
}
//拨打电话
tools.playphone = (phoneNumber) => {
	uni.makePhoneCall({
		// 手机号 这里可以直接写号码如 12345 也可以写获取号码的字段如this.mobile
		phoneNumber,
		// 成功回调
		success: (res) => {
			console.log('调用成功!')
		},
		// 失败回调
		fail: (res) => {
			console.log('调用失败!')
		}
	});
}
//时间戳转年月日  参数是秒的时间戳 函数返回一个对象 对象里有年 月 日
tools.yearDay = (long) => {
	var time = new Date(long);
	var year = time.getFullYear();
	var month = time.getMonth() + 1 < 10 ? '0' + (time.getMonth() + 1) : time.getMonth() + 1;
	var day = time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
	var week = time.getDay(); //获取星期
	var obj = {};
	switch (week) {
		case 0:
			obj.str = "周日";
			obj.str1 = "Sun"
			break;
		case 1:
			obj.str = "周一";
			obj.str1 = "Mon"
			break;
		case 2:
			obj.str = "周二";
			obj.str1 = "Tue"
			break;
		case 3:
			obj.str = "周三";
			obj.str1 = "Wed"
			break;
		case 4:
			obj.str = "周四";
			obj.str1 = "Thur"
			break;
		case 5:
			obj.str = "周五";
			obj.str1 = "Fri"
			break;
		case 6:
			obj.str = "周六";
			obj.str1 = "Sat"
			break;

	}
	var yearday = {
		year,
		month,
		day,
		obj
	};
	return yearday;
}

tools.whichWeek = (year) => {
	var d = new Date(year, 0, 1);
	while (d.getDay() != 1) {
		d.setDate(d.getDate() + 1);
	}
	let arr = [];
	let longnum = d.setDate(d.getDate());
	if (longnum > +new Date(year, 0, 1)) {
		let obj = tools.yearDay(+new Date(year, 0, 1) / 1000);
		obj.last = tools.yearDay(longnum / 1000 - 86400);
		arr.push(obj);
	}
	let oneitem = tools.yearDay(longnum / 1000);
	oneitem.last = tools.yearDay(longnum / 1000 + 86400 * 6);
	arr.push(oneitem);
	var lastStr;
	for (var i = 0; i < 51; i++) {
		let long = d.setDate(d.getDate() + 7);
		let obj = tools.yearDay(long / 1000);
		obj.last = tools.yearDay(long / 1000 + 86400 * 6);
		lastStr = long + 86400000 * 6;
		arr.push(obj);
	}
	if (lastStr < +new Date(year + 1, 0, 1)) {
		let obj = tools.yearDay(lastStr / 1000 + 86400);
		obj.last = tools.yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400);
		arr.push(obj);
	} else {
		arr[arr.length - 1].last = tools.yearDay(+new Date(year + 1, 0, 1) / 1000 - 86400);
	}
	return arr;
}
//将时间戳转化为固定格式
tools.formatTime = (value, type) => {
	var dataTime = "";
	var data = new Date();
	data.setTime(value);
	var year = data.getFullYear();
	var month_temp = data.getMonth() + 1;
	var month = month_temp < 10 ? '0' + month_temp : month_temp;
	var day = data.getDate() < 10 ? '0' + data.getDate() : data.getDate();
	var hour = data.getHours() < 10 ? '0' + data.getHours() : data.getHours();
	var minute = data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes();
	var second = data.getSeconds() < 10 ? '0' + data.getSeconds() : data.getSeconds();
	var rangeDateNum = Math.floor(data.getTime() / (1000 * 3600 * 24))
	if (type == "YMD") {
		dataTime = year + "-" + month + "-" + day;
	} else if (type == "DHMS") {
		dataTime = `${rangeDateNum}天${hour}时${minute}分${second}秒`
	} else if (type == "YMDHMS") {
		dataTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
	} else if (type == "HMS") {
		dataTime = hour + ":" + minute + ":" + second;
	} else if (type == "YM") {
		dataTime = year + "-" + month;
	}
	return dataTime; //将格式化后的字符串输出到前端显示
}

tools.formatTime2 = (value) => {
		var timeValue = value.replace(/-/g, "/");
		var T_pos = timeValue.indexOf('T');
		var year_month_day = timeValue.substr(0, T_pos);
		var hour_minute_second = timeValue.substr(T_pos + 1, 8);
		var new_datetime = year_month_day + " " + hour_minute_second;
		var dateee = new Date(new_datetime).toJSON();
		var sjc = new Date(dateee).getTime();
		var chinaT = sjc + 8 * 3600 * 1000;
		var date = tools.formatTime(chinaT, 'YMDHMS');
		return date;
	},

	// 将固定时间格式转换成时间戳
	tools.formatTime3 = (value) => {
		var data = new Date(value)
		var timestamp = data.getTime()
		return timestamp
	}

// 获取时间戳
tools.formatTime4 = (type) => { //1为毫秒  2为秒
	var data = new Date()
	if (type == 1) {
		var timestamp = data.getTime()
	}
	if (type == 2) {
		var timestamp = data.getTime() / 1000
	}
	return timestamp
}

//保留小数点
tools.number = (value, type) => {
	if (value == null || value == '') {
		value = 0;
	}
	//处理科学计数法
	value = value.toFixed(8);
	var arr = (value + "").split(".");
	var a1 = arr[0];
	var a2 = 0;
	if (arr.length > 1) {
		a2 = arr[1];
		if (a2.length > type) {
			a2 = a2.substring(0, type);
		}
	}
	value = Number(a1 + "." + a2);
	return Number(value).toFixed(type)
}

tools.prePage = () => {
	let pages = getCurrentPages();
	let prePage = pages[pages.length - 2];
	// #ifdef H5
	return prePage;
	// #endif
	return prePage.$vm;
}

export default tools